#include "Table.h"
#include "Hand.h"
#include <stdlib.h>

Table::Table()
{
    _Single = new Foundation(Foundation::SINGLE);
    _Double = new Foundation(Foundation::DOUBLE);
    _Triple = new Foundation(Foundation::TRIPLE);
    _Quadruple = new Foundation(Foundation::QUADRUPLE);

    _tableau_1 = new Deck();
    _tableau_2 = new Deck();
    _tableau_3 = new Deck();
    _tableau_4 = new Deck();

    _hand = new Hand();
    _card = NULL;

    Initialize();
}

Table::~Table()
{
    delete _Single;
    delete _Double;
    delete _Triple;
    delete _Quadruple;

    delete _tableau_1;
    delete _tableau_2;
    delete _tableau_3;
    delete _tableau_4;

    delete _hand;
    delete _card;
}

void Table::Initialize()
{
    Deck* aux = new Deck();

    bool aceFound = false,
         twoFound = false,
         threeFound = false,
         fourFound = false;

    for( int i = 0; i<_hand->getSize(); ++i)
    {
        Card* card = _hand->remove();
        int value = card->getValue();

        if( value == 1 && !aceFound )
        {
            aceFound = true;
            _Single->insert(card);
        }
        else if( value == 2 && !twoFound )
        {
            twoFound = true;
            _Double->insert(card);
        }
        else if( value == 3 && !threeFound )
        {
            threeFound = true;
            _Triple->insert(card);
        }
        else if( value == 4 && !fourFound )
        {
            fourFound = true;
            _Quadruple->insert(card);
        }
        else
        {
            aux->insert(card);
        }
    }

    for( int i = 0; i < aux->getSize(); ++i )
    {
        Card* card = aux->remove();
        _hand->insert(card);
    }

    delete aux;
}
